import { join } from 'path'
import { NestFactory } from '@nestjs/core'
import { NestExpressApplication } from '@nestjs/platform-express'
import { getCwd, initialSSRDevProxy, loadConfig } from 'ssr-common-utils'

import { AppModule } from './app.module'

async function bootstrap(): Promise<void> {
	const app = await NestFactory.create<NestExpressApplication>(AppModule)
	await initialSSRDevProxy(app, {
		express: true
	})
	app.useStaticAssets(join(getCwd(), './build'))
	app.useStaticAssets(join(getCwd(), './build/client'))
	app.useStaticAssets(join(getCwd(), './public'))
	const { serverPort } = loadConfig()
	await app.listen(serverPort)
}

bootstrap().catch((err) => {
	console.log(err)
	process.exit(1)
})
